package myfiles

import (
	"os"
  "strconv"
  "strings"
)

var Media = []string{"mp4", "flv", "avi", "mpeg", "wmv", "m4v", "mov", "f4v"}
var Music = []string{"flac", "mp3", "m4a"}
var Image = []string{"jpeg", "jpg", "png"}



func Exists(path string) bool {  
    _, err := os.Stat(path)
    if err != nil {  
        if os.IsExist(err) {  
            return true  
        }  
        return false  
    }  
    return true  
}  

func IsDir(path string) bool {  
    s, err := os.Stat(path)  
    if err != nil {  
        return false  
    }  
    return s.IsDir()  
}  
  
 
func IsFile(path string) bool {  
    return !IsDir(path)  
}


func FileSuffix(filename string) (string, string) {
    list := strings.Split(filename, ".")
    suffix := list[len(list)-1]
    var k = int64(1)
    for _, value := range [][]string{Media, Music, Image} {
        for _, val := range value {
            if val == suffix {
                goto Loop
            }
        }
        k++
    }
    Loop:
    return strconv.FormatInt(k, 10), suffix
}


func DirList(path string) []string {
    var file_list []string
    f, err := os.Open(path)
    defer f.Close()
    if err != nil {
        return file_list
    }
    files, err := f.Readdir(-1)
    if err != nil {
        return file_list
    }
    for _, file := range files {
        file_list = append(file_list, file.Name())
    }
    return file_list
}


func Isimage(path string) bool {
    if Exists(path) {
        _type, _ := FileSuffix(path)
        if _type == "3" {
            return true
        }
    }
    return false
}


func Tofloat64(f string) float64 {
  if s, err := strconv.ParseFloat(f, 64); err == nil {
      return s
  }
  return float64(0)
}

func Toint64(f string) int64 {
  if s, err := strconv.ParseInt(f, 10, 64); err == nil {
      return s
  }
  return int64(0)
}